package TestClass;

import com.alibaba.fastjson.JSON;
import model.Student;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@WebServlet("/ajax3")
public class ajax3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<User> users = new ArrayList<>();
        User user1 = new User();
        user1.setUser("aa");
        user1.setPw("12345");
        User user2 = new User();
        user2.setUser("bb");
        user2.setPw("123456");
        User user3 = new User();
        user3.setUser("cc");
        user3.setPw("123456");

        users.add(user1);
        users.add(user2);
        users.add(user3);

        String res = JSON.toJSONString(users);
        System.out.println("后端json格式的字符串="+res);
        //传回前端json格式的字符串key--value

        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");

        PrintWriter writer = resp.getWriter();
        writer.write(res);
        //释放缓存和对象
        writer.flush();
        writer.close();


    }
}
